package drds.global_transaction.common.util;

import drds.common.$;
import drds.common.ShouldNeverHappenException;
import lombok.extern.slf4j.Slf4j;

import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.sql.SQLException;

@Slf4j
public class Blobs
{

	public static String blob2string(Blob blob) throws SQLException
	{
		if (blob == null)
		{
			return null;
		}

		return new String(blob.getBytes((long) 1, (int) blob.length()));
	}

	public static Blob string2blob(String string)
	{
		if (string == null)
		{
			return null;
		}

		try
		{
			return new SerialBlob(string.getBytes());
		} catch (Exception e)
		{
			log.error($.printStackTraceToString(e));
			throw new ShouldNeverHappenException();
		}
	}

	private Blobs()
	{

	}
}
